package persistencia;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author daniel
 */
public class BDPadrao {

    //driver = "org.postgresql.Driver"
    //url    = "jdbc:postgresql:<IP>:<Porta>/<Banco>
    //exemplo: "jdbc:postgresql://localhost:5432/senac"
    final static String URL = "jdbc:postgresql://localhost:5432/mic";
    final static String USUARIO = "postgres"; //alterar login do banco de dados
    final static String SENHA = "123"; //alterar senha do banco de dados

    
    static protected Connection conectar() throws SQLException, ClassNotFoundException {
        //Mapeamento da Classe de Conexão do Postgres
        Class.forName("org.postgresql.Driver");
        //Objeto conexao = estabelece uma ponte entre a aplicação e o BD
        Connection conexao = DriverManager.getConnection(URL, USUARIO, SENHA);
        return conexao;
    }

    static protected void desconectar(Connection conn) throws SQLException{
        conn.close();
    }
    
    static protected ResultSet pesquisar(String sql) throws SQLException, ClassNotFoundException {

        Connection conn = conectar();

        Statement stm = conn.createStatement();
        ResultSet rs = stm.executeQuery(sql);

        desconectar(conn);
        
        return rs;
    }

}
